package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.BsComplain;
import com.ruoyi.system.domain.ro.BsComplainRO;
import com.ruoyi.system.mapper.BsComplainMapper;
import com.ruoyi.system.service.IBsComplainService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 投诉信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-20
 */
@RequiredArgsConstructor
@Service
public class BsComplainServiceImpl implements IBsComplainService {

    private final BsComplainMapper baseMapper;

    /**
     * 查询投诉信息
     */
    @Override
    public BsComplain queryById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 查询投诉信息列表
     */
    @Override
    public List<BsComplain> queryList(BsComplainRO bo) {
        LambdaQueryWrapper<BsComplain> lqw = buildQueryWrapper(bo);
        return baseMapper.selectList(lqw);
    }

    private LambdaQueryWrapper<BsComplain> buildQueryWrapper(BsComplainRO ro) {
        Map<String, Object> params = ro.getParams();
        LambdaQueryWrapper<BsComplain> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(ro.getType()), BsComplain::getType, ro.getType());
        lqw.eq(StringUtils.isNotBlank(ro.getAddress()), BsComplain::getAddress, ro.getAddress());
        lqw.eq(StringUtils.isNotBlank(ro.getComplainFor()), BsComplain::getComplainFor, ro.getComplainFor());
        lqw.eq(StringUtils.isNotBlank(ro.getComplainReason()), BsComplain::getComplainReason, ro.getComplainReason());
        lqw.eq(StringUtils.isNotBlank(ro.getComplainImg()), BsComplain::getComplainImg, ro.getComplainImg());
        lqw.like(StringUtils.isNotBlank(ro.getComplainName()), BsComplain::getComplainName, ro.getComplainName());
        lqw.eq(StringUtils.isNotBlank(ro.getComplainPhone()), BsComplain::getComplainPhone, ro.getComplainPhone());
        lqw.eq(StringUtils.isNotBlank(ro.getHandleComplainType()), BsComplain::getHandleComplainType, ro.getHandleComplainType());
        lqw.eq(StringUtils.isNotBlank(ro.getHandleReturnVisit()), BsComplain::getHandleReturnVisit, ro.getHandleReturnVisit());
        lqw.eq(StringUtils.isNotBlank(ro.getHandleRemark()), BsComplain::getHandleRemark, ro.getHandleRemark());
        lqw.eq(StringUtils.isNotBlank(ro.getHandleResult()), BsComplain::getHandleResult, ro.getHandleResult());
        lqw.eq(StringUtils.isNotBlank(ro.getHandleNeedAgain()), BsComplain::getHandleNeedAgain, ro.getHandleNeedAgain());
        lqw.eq(StringUtils.isNotBlank(ro.getHandleBy()), BsComplain::getHandleBy, ro.getHandleBy());
        lqw.eq(StringUtils.isNotBlank(ro.getHandleStatus()), BsComplain::getHandleStatus, ro.getHandleStatus());
        lqw.eq(ro.getHandleTime() != null, BsComplain::getHandleTime, ro.getHandleTime());
        lqw.eq(ro.getCreateById() != null, BsComplain::getCreateById, ro.getCreateById());
        lqw.between(params.get("beginCreateTime") != null && StringUtils.isNotBlank(params.get("beginCreateTime").toString()) && params.get("endCreateTime") != null && StringUtils.isNotBlank(params.get("endCreateTime").toString()),
                BsComplain::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByDesc(BsComplain::getCreateTime);
        return lqw;
    }

    /**
     * 新增投诉信息
     */
    @Override
    public Boolean insertByBo(BsComplainRO bo) {
        BsComplain add = BeanUtil.toBean(bo, BsComplain.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改投诉信息
     */
    @Override
    public Boolean updateByBo(BsComplainRO bo) {
        BsComplain update = BeanUtil.toBean(bo, BsComplain.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BsComplain entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除投诉信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
